package com.auxgroup.smarthome.inner.entity;


import com.alibaba.fastjson.JSONObject;
import com.auxgroup.bridge.business.inner.dto.AppPushTemplateDto;
import com.auxgroup.jpush.entity.JpushEntity;
import com.auxgroup.smarthome.BeanUtils;
import com.auxgroup.smarthome.entity.AbstractGenericEntity;
import com.auxgroup.smarthome.utils.NewDateUtils;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.util.Date;

/**
 * Created by niuGuangzhe on 2017/8/7.
 */
@Entity
@Table(name = "app_push_entity")
public class AppPushTemplateEntity extends AbstractGenericEntity {
    @Id
    @GeneratedValue(generator = "uuid2")
    @GenericGenerator(name = "uuid2", strategy = "uuid2")
    @Column(length = 36, columnDefinition = "VARCHAR(36) COMMENT '推送模板主键'")
    private String templateId;

    @Column(columnDefinition = "VARCHAR(64) NOT NULL COMMENT '模板标题'")
    private String templateTitle;

    @Lob
    @Column(columnDefinition = "TEXT NOT NULL COMMENT '模板内容' ")
    private String templateContent;

    @Lob
    @Column(columnDefinition = "TEXT NOT NULL COMMENT '推送条件json'")
    private String templateCondition;

    @Lob
    @Column(columnDefinition = "TEXT COMMENT '模板所在位置url'")
    private String templateUri;


    @Column(columnDefinition = "INTEGER NOT NULL COMMENT '推送类型 0：立即推送 1:定时推送'")
    private Integer pushType;

    @Column(columnDefinition = "DATETIME COMMENT '推送时间 严格按照 yyyy-MM-dd HH:mm'")
    private Date pushTime;

    @Column(columnDefinition = "INTEGER NOT NULL DEFAULT 0 COMMENT '状态 0：已保存 1:已发送 2：已删除'")
    private Integer status;

    @Lob
    @Column(columnDefinition = "TEXT COMMENT '图片链接'")
    private String imgUri;

    @Column(columnDefinition = "VARCHAR(32) COMMENT '模板类型 notice：通知 advertisement：广告'")
    private String templateType;

    @Lob
    @Column(columnDefinition = "TEXT  COMMENT '广告json'")
    private String adCondition;

    public String getTemplateId() {
        return templateId;
    }

    public void setTemplateId(String templateId) {
        this.templateId = templateId;
    }

    public String getTemplateTitle() {
        return templateTitle;
    }

    public void setTemplateTitle(String templateTitle) {
        this.templateTitle = templateTitle;
    }

    public String getTemplateContent() {
        return templateContent;
    }

    public void setTemplateContent(String templateContent) {
        this.templateContent = templateContent;
    }

    public String getTemplateCondition() {
        return templateCondition;
    }

    public void setTemplateCondition(String templateCondition) {
        this.templateCondition = templateCondition;
    }

    public String getTemplateUri() {
        return templateUri;
    }

    public void setTemplateUri(String templateUri) {
        this.templateUri = templateUri;
    }

    public Integer getPushType() {
        return pushType;
    }

    public void setPushType(Integer pushType) {
        this.pushType = pushType;
    }

    public Date getPushTime() {
        return pushTime;
    }

    public void setPushTime(Date pushTime) {
        this.pushTime = pushTime;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    /**
     * dto转换实体类型
     *
     * @param dto
     * @param status
     * @return
     */
    public static AppPushTemplateEntity translateToEntity(AppPushTemplateDto dto, Integer status) {
        AppPushTemplateEntity entity = BeanUtils.copyAttrs(new AppPushTemplateEntity(), dto);
        if (dto.getTemplateCondition() == null) {
            entity.setTemplateCondition("{\"citys\":{},\"gender\":\"N\"}");
        }

        entity.setStatus(status);
        return entity;
    }


    /**
     * 转换成为发送类型
     *
     * @return
     */
    public JpushEntity translateToJPushEntity() {
        JpushEntity jpushEntity = new JpushEntity(this.templateTitle, this.templateContent, this.pushTime == null ? null : NewDateUtils.formatDateTime(this.pushTime));
        jpushEntity.setJpushMessageUrl(this.templateUri);
        jpushEntity.setImageUrl(this.imgUri);
        jpushEntity.setType("jump");
        JSONObject adObject = JSONObject.parseObject(this.adCondition);
        jpushEntity.setPlatform(adObject.getString("platform"));
        jpushEntity.setSourceType(adObject.getString("sourceType"));
        jpushEntity.setSourceValue(adObject.getString("sourceValue"));

        return jpushEntity;
    }

    public String getImgUri() {
        return imgUri;
    }

    public void setImgUri(String imgUri) {
        this.imgUri = imgUri;
    }

    public String getTemplateType() {
        return templateType;
    }

    public void setTemplateType(String templateType) {
        this.templateType = templateType;
    }

    public String getAdCondition() {
        return adCondition;
    }

    public void setAdCondition(String adCondition) {
        this.adCondition = adCondition;
    }
}